package com.ihr360.payroll.service.salarytask.validator;

import com.ihr360.authority.sharing.validator.GetOneValidator;
import com.ihr360.payroll.dao.ActivePayPeriodRepository;
import com.ihr360.payroll.dao.PayPeriodTermRepository;
import com.ihr360.payroll.model.dto.salary.plan.SalaryPlan;
import com.ihr360.shared.validator.SessionValidator;
import com.ihr360.validation.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PayPeriodTermAuthValidator extends SessionValidator<Long> {
    @Autowired
    private GetOneValidator getOneValidator;
    @Autowired
    private PayPeriodTermRepository payPeriodTermRepository;
    @Autowired
    private ActivePayPeriodRepository activePayPeriodRepository;

    @Override
    public boolean supports(Class<?> clazz) {
        return super.supports(clazz) && Long.class.equals(clazz);
    }

    @Override
    public void validate(Long termId) {
        Long payPeriodId = payPeriodTermRepository.queryPayperiodId(termId);
        Long salaryPlanId = activePayPeriodRepository.querySalaryPlanId(payPeriodId);
        ValidationUtils.rejectIfEmpty(salaryPlanId, null, "操作数据不存在");
        getOneValidator.validate(salaryPlanId, SalaryPlan.class);
    }
}